package com.cheyun.netsalev3.view.cluefollowup

import android.content.Intent
import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.MyApplication
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.databinding.FragmentClueFollowUpBinding
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.view.AddClueActivity
import com.cheyun.netsalev3.view.defeatdetails.DefeatDetailsActivity
import com.cheyun.netsalev3.viewmodel.ClueFollowUpActivityViewModel

class ClueFollowUpFragment : BaseFragment<FragmentClueFollowUpBinding>() {
    override fun initContentView(): Int {
        return R.layout.fragment_clue_follow_up
    }

    override fun initViewModel(): ViewModel? {
        activity?.apply {
            return ViewModelProviders.of(this).get(ClueFollowUpActivityViewModel::class.java)
        }
        return super.initViewModel()
    }

    private lateinit var adapter: MyAdapter

    override fun initView() {
        adapter = MyAdapter(R.layout.clue_follow_up_item, binding.viewModel)
        binding.recyclerView.adapter = adapter

        binding.headview.rightBut?.setOnClickListener {
            binding.viewModel?.enableChangeDrivetime()
        }

        val goDetail = Observer<Boolean> { flag ->
            if(flag) {
                var list = MyApplication.getInstance().getAcitvitys()
                for (i in list) {
                    if (i is AddClueActivity) {
                        i.finish()
                    }
                }
                var intent = Intent(context,DefeatDetailsActivity::class.java)
                intent.putExtra("tid", binding.viewModel?.data?.tid)
                intent.putExtra("nkey", "archive.purge")
                intent.putExtra("come","")
                this.startActivity(intent)
            }
        }
        binding.viewModel?.operComplete?.observe(this,goDetail)
    }

    override fun initData() {
        binding.viewModel?.viewList?.observe(this, Observer { list ->
            adapter.submitList(list)
        })

        binding.viewModel?.followBtnEnable?.observe(this, Observer {
            binding.nexBut.isEnabled = it
        })
        binding.headview.rightBut?.visibility =  if(binding.viewModel?.data?.reserveid.equals("0") || binding.viewModel?.data?.reserveid.isNullOrEmpty()) View.GONE else View.VISIBLE
    }

    override fun lazyInit() {
        Log.i("ClueFollowUpFragment","override lazyinit")
    }
}
